﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FirmaTransportowa.Presenters
{
    class CashierPackageDetailsPresenter
    {
        CashierPanelPackageDetails view;
        CashierPanelPresenter presenter;

        public CashierPackageDetailsPresenter(CashierPanelPresenter presenter, Package package = null)
        {
            this.presenter = presenter;
            this.view = new CashierPanelPackageDetails(null, package);

            this.view.RecipientIndexChanged += OnRecipientChanged;
            this.view.SenderIndexChanged += OnSenderChanged;
            this.view.ResetClicked += OnResetClicked;
            this.view.SaveClicked += OnSaveClicked;

            view.listBox1.DataSource = Sender.list();
            view.listBox2.DataSource = Recipient.list();

            this.view.Show();
        }

        public bool verifyForm()
        {
            bool isFormCorrect = true;

            if (view.tbSenderName.Text.Trim() == String.Empty)
            {
                view.labelSenderNameError.Visible = true;
                isFormCorrect = false;
            }
            if (view.tbSenderSurname.Text.Trim() == String.Empty)
            {
                view.labelSenderSurnameError.Visible = true;
                isFormCorrect = false;
            }
            if (view.tbSenderPostcode.Text.Trim() == String.Empty)
            {
                view.labelSenderPostcodeError.Visible = true;
                isFormCorrect = false;
            }
            if (view.tbSenderCity.Text.Trim() == String.Empty)
            {
                view.labelSenderCityError.Visible = true;
                isFormCorrect = false;
            }
            if (view.tbSenderStreet.Text.Trim() == String.Empty)
            {
                view.labelSenderStreetError.Visible = true;
                isFormCorrect = false;
            }
            if (view.tbSenderHouseNumber.Text.Trim() == String.Empty)
            {
                view.labelSenderHouseNumberError.Visible = true;
                isFormCorrect = false;
            }
            if (view.tbSenderPhone.Text.Trim() == String.Empty)
            {
                view.labelSenderPhoneError.Visible = true;
                isFormCorrect = false;
            }
            if (view.tbRecipientName.Text.Trim() == String.Empty)
            {
                view.labelRecipientNameError.Visible = true;
                isFormCorrect = false;
            }
            if (view.tbRecipientSurname.Text.Trim() == String.Empty)
            {
                view.labelRecipientSurnameError.Visible = true;
                isFormCorrect = false;
            }
            if (view.tbRecipientPostcode.Text.Trim() == String.Empty)
            {
                view.labelRecipientPostcodeError.Visible = true;
                isFormCorrect = false;
            }
            if (view.tbRecipientCity.Text.Trim() == String.Empty)
            {
                view.labelRecipientCityError.Visible = true;
                isFormCorrect = false;
            }
            if (view.tbRecipientStreet.Text.Trim() == String.Empty)
            {
                view.labelRecipientStreetError.Visible = true;
                isFormCorrect = false;
            }
            if (view.tbRecipientHouseNumber.Text.Trim() == String.Empty)
            {
                view.labelRecipientHouseNumberError.Visible = true;
                isFormCorrect = false;
            }
            if (view.tbRecipientPhone.Text.Trim() == String.Empty)
            {
                view.labelRecipientPhoneError.Visible = true;
                isFormCorrect = false;
            }
            if (view.tbPackageWeight.Text.Trim() == String.Empty)
            {
                view.labelWightError.Visible = true;
                isFormCorrect = false;
            }

            if (isFormCorrect)
            {
                return true;
            }
            return false;
        }

        private void saveOrUpdate()
        {
            if (verifyForm())
            {
                Sender sender = new Sender();
                sender.id = view.currentPackage!=null? view.currentPackage.sender.id : -1;
                sender.name = view.tbSenderName.Text;
                sender.surname = view.tbSenderSurname.Text;
                sender.postcode = view.tbSenderPostcode.Text;
                sender.city = view.tbSenderCity.Text;
                sender.street = view.tbRecipientStreet.Text;
                sender.house_number = view.tbSenderHouseNumber.Text;
                sender.phone = view.tbSenderPhone.Text;

                Recipient recipient = new Recipient();
                recipient.id=view.currentPackage != null ? view.currentPackage.recipient.id : -1;
                recipient.name = view.tbRecipientName.Text;
                recipient.surname = view.tbRecipientSurname.Text;
                recipient.postcode = view.tbRecipientPostcode.Text;
                recipient.city = view.tbRecipientCity.Text;
                recipient.street = view.tbRecipientStreet.Text;
                recipient.house_number = view.tbRecipientHouseNumber.Text;
                recipient.phone = view.tbRecipientPhone.Text;

                
                int newSenderId = sender.save();
                int newRecipientId = recipient.save();

                if (newSenderId == -1) newSenderId = Sender.getIdByNameAndSurname(view.tbSenderName.Text, view.tbSenderSurname.Text);
                if (newRecipientId == -1) newRecipientId = Recipient.getIdByNameAndSurname(view.tbRecipientName.Text, view.tbRecipientSurname.Text);

                Package package = new Package();
                package.id = view.currentPackage != null ? view.currentPackage.id : -1;
                package.status = PackageStatus.NEW;
                package.weight = float.Parse(view.tbPackageWeight.Text);
                package.createDate = DateTime.Now;
                package.sender = Sender.getById(newSenderId);
                package.recipient = Recipient.getById(newRecipientId);
                if (view.currentPackage != null)
                {
                    package.firstTransport = view.currentPackage.firstTransport;
                    package.secondTransport = view.currentPackage.secondTransport;
                }

                package.save();

                view.Close();
                this.presenter.refresh();
            }

        }

        private void OnRecipientChanged()
        {
            int id = -1;
            Int32.TryParse(view.listBox2.SelectedValue.ToString(), out id);
            Recipient recipient = Recipient.getById(id);
            view.fillRecipient(recipient);
        }

        private void OnSenderChanged()
        {
            int id = -1;
            Int32.TryParse(view.listBox1.SelectedValue.ToString(), out id);
            Sender sender = Sender.getById(id);
            view.fillSender(sender);
        }

        private void OnResetClicked()
        {
            view.fillSender(view.currentPackage.sender);
            view.fillRecipient(view.currentPackage.recipient);
        }

        private void OnSaveClicked()
        {
            if (verifyForm())
            {
                saveOrUpdate();
            }
        }
    }
}
